<template>
	<view class="nav-box" :style="{ 'height': height + 'px', 'background': bgColor }" @click="cli"
		:addressNow="addressNow">
		<!-- 自定义导航栏 -->
		<view class="status_bar" :style="{ 'height': statusBarHeight + 'px' }">
			<!-- uni-ui这里是状态栏 -->
		</view>
		<!-- 胶囊位置信息 -->
		<view class="nav-main flex align-center justify-center" :style="{ height: navBarHeight + 'px' }">
			<view class="nav-main-back" @click="back" v-if="backIcon">
				<!-- <uni-icons type="back" size="26" color="#fff"></uni-icons> -->
			</view>
			<view class="nav-main-title">
				<image src="http://sni1muz85.hd-bkt.clouddn.com/img/icons/address.png"></image>
				<text>{{title}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			bgColor: {
				type: String,
				default: "#fff"
			},
			backIcon: {
				type: Boolean,
				default: true
			},
			title: {
				type: String,
				default: "当前位置"
			}
		},
		data() {
			return {
				//总高度
				height: 0,
				//胶囊位置信息
				menuButtonRect: {},
				//状态栏的高度
				statusBarHeight: 0,
				//导航栏的高度
				navBarHeight: 0,
				addressNow: ""
			}
		},
		created() {
			// this.height = wx.getStorageSync('navBarHeight')
			this.getHeight();
			// this.cli()
		},
		methods: {

			cli() {
				let that = this
				uni.chooseLocation({
					// type: 'wgs84',
					success: function(res) {
						// that.addressNow=res.address
						that.$emit('handlerChange', res);
					}
				});
			},
			//获取屏幕导航栏高度
			getHeight() {
				if (wx.canIUse('getMenuButtonBoundingClientRect')) {
					let sysInfo = wx.getSystemInfoSync(); //状态栏的高度
					this.statusBarHeight = sysInfo.statusBarHeight;
					// 胶囊位置信息
					let rect = wx.getMenuButtonBoundingClientRect();
					this.menuButtonRect = JSON.parse(JSON.stringify(rect));
					// 导航栏高度
					let navBarHeight = (rect.top - sysInfo.statusBarHeight) * 2 + rect.height;
					this.navBarHeight = navBarHeight;
					// 自定义导航栏的高度
					this.height = sysInfo.statusBarHeight + navBarHeight;
				} else {
					wx.showToast({
						title: '您的微信版本过低，界面可能会显示不正常',
						icon: 'none',
						duration: 4000
					});
				}
			},
			//返回
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	.status_bar {
		// height: var(--status-bar-height);
		width: 100vw;
		// background:#fff;
	}

	.nav-main {
		position: relative;

		// background:#fff;
		.nav-main-back {
			position: absolute;
			left: 10rpx;
		}

		.nav-main-title {
			display: flex;
			align-items: center;
			// background-color: #fff;
			padding: 20rpx 0;

			image {
				height: 35rpx;
				width: 35rpx;
				margin-right: 20rpx;
				margin-left: 20rpx;
			}

			text {
				width: 600rpx;
				font-size: 32rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				color: #787878;
			}
		}
	}
</style>